/*
 * @Author: szx
 * @Date: 2021-12-22 15:37:00
 * @LastEditTime: 2021-12-22 15:59:29
 * @Description:
 * @FilePath: \leetcode\500-599\513\513.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
// var findBottomLeftValue = function (root) {
//     const que = [root];
//     let res = 0;
//     while (que.length) {
//         let len = que.length;
//         for (let i = 0; i < len; i++) {
//             const node = que.shift();
//             if (i == 0) res = node.val;
//             if (node.left) que.push(node.left);
//             if (node.right) que.push(node.right);
//         }
//     }
//     return res;
// };

var findBottomLeftValue = function (root) {
    let maxLen = -Infinity;
    let res = 0;
    const traversal = (root, len) => {
        root.left && traversal(root.left, len + 1);
        root.right && traversal(root.right, len + 1);
        if (!root.left && !root.right) {
            if (len > maxLen) {
                maxLen = len;
                res = root.val;
            }
        }
    };
    traversal(root, 0);
    return res;
};
